package org.example.study2;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedStreamExample {
    public static void main(String[] args) {
        // 使用try-with-resources语句自动关闭流
        try (FileOutputStream fos = new FileOutputStream("D:/temp/buffered_output.txt");
             BufferedOutputStream bos = new BufferedOutputStream(fos)) { // 装饰者模式

            // 循环写入大量数据
            for (int i = 0; i < 100000; i++) {
                bos.write(("Line " + i + "\n").getBytes());
            }
            System.out.println("大量数据写入完成（高效）！");
            // try-with-resources块结束时会自动调用bos.close()和fos.close()，无需手动写
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
